import Vue from 'vue'
import Router from 'vue-router'
/**
 * 路由定义规范：
 * 1、子功能的路由需要以父功能的路由开头，这个是为了：统一收集错误信息时的菜单路径
 */

/**
 * 重写路由的push方法
 */
const routerPush = Router.prototype.push;
Router.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error=> error)
};

Vue.use(Router);
let routes = new Set();

const getChunks = (modulesContext) => {
    if (!modulesContext || modulesContext.length === 0) {
        return undefined;
    }
    return modulesContext.keys().reduce((modules, key) => {
        modules[key.replace(/(^\.\/)|(\.js$)/g, '')] = modulesContext(key).default
        return modules;
    }, {});
}
// 自动导入路由
const routerContext = require.context('./modules', true, /\.js$/);
const routerChunks = getChunks(routerContext)
Object.keys(routerChunks).forEach((item) => {
    let paths = routerChunks[item];
    Object.keys(paths).forEach(index => {
        routes.add(paths[index]);
    });
})

export default routes;
